package ee.ut.maxorato_macobo.oop.mud.items;

import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Scanner;

import ee.ut.maxorato_macobo.oop.mud.game.DataParser;
import ee.ut.maxorato_macobo.oop.mud.game.GameUtils;
import ee.ut.maxorato_macobo.oop.mud.game.PlayerCharacter;

public class ItemContainer extends ItemBase implements ContainsItems {
	private ArrayList<ItemBase> contents;
	private ItemContainerTemplate itemTemplate;
	
	public ItemContainer(ItemTemplate newItemTemplate, ContainsItems newItemLocation) {
		super(newItemTemplate, newItemLocation);
		
		contents = new ArrayList<ItemBase>();
		itemTemplate = (ItemContainerTemplate)newItemTemplate;
	}
	
	@Override
	public void destroyItem() {
		ArrayList<ItemBase> itemList = new ArrayList<ItemBase>(contents);
		
		for(ItemBase item : itemList) {
			item.destroyItem();
		}
		
		super.destroyItem();
	}
	
	public void parseExtraSection(Scanner parser) {
		while(parser.hasNextLine()) {
			String[] parts = parser.nextLine().trim().split(" ", 2);
			
			if(parts[0].equals("sub")) {
				if(parts[1].equals("itemi")) {
					ItemBase subItem = DataParser.parseItemInstInfo(parser, true);
					
					if(subItem != null) {
						subItem.moveToLocation(this);
					}
				}
				else {
					DataParser.parseDummy(parser);
				}
			}
			else if(parts[0].equals("end")) {
				break;
			}
		}
	}
	
	public void describeForPlayer(PlayerCharacter player) {
		player.sendTickMessage(getLookString() + "\r\n" + getDescription());
		
		player.sendTickMessage("\r\nIt contains the following items:");
		GameUtils.describeItemListForPlayer(player, getItemList(), false);
	}
	
	@Override
	public ItemBase clone() {
		ItemContainer container = new ItemContainer(itemTemplate, itemLocation);
		
		ArrayList<ItemBase> itemList = new ArrayList<ItemBase>(getItemList());
		
		for(ItemBase item : itemList) {
			item.clone().moveToLocation(container);
		}

		return container;
	}

	public ArrayList<ItemBase> getItemList() {
		return contents;
	}
	
	public String canAddItem(ItemBase item) {
		if(item.getLocation() == this || contents.size() < itemTemplate.getMaxItems()) {
			return null;
		}
		else {
			return "Container cannot store any more items.";
		}
	}
	
	public void itemAdd(ItemBase item) {
		if(!contents.contains(item)) {
			contents.add(item);
		}
	}
	
	public void itemRemove(ItemBase item) {
		if(contents.contains(item)) {
			contents.remove(item);
		}
	}
	
	public ItemBase findItemByName(String itemName) {
		return findItemByName(itemName, 1);
	}
	
	public ItemBase findItemByName(String itemName, int index) {
		ListIterator<ItemBase> it = getItemList().listIterator();
		int matchCount = 0;
		
		while(it.hasNext()) {
			ItemBase item = it.next();
			
			if(item.getName().compareToIgnoreCase(itemName) == 0) {
				
				if(++matchCount == index) {
					return item;
				}
			}
		}
		
		return null;
	}
	
	public int getWeight() {
		int weight = itemTemplate.getWeight();
		
		ListIterator<ItemBase> it = contents.listIterator();
		
		while(it.hasNext()) {
			weight += it.next().getWeight();
		}
		
		return weight;
	}
}
